﻿Imports Oracle.DataAccess.Client
Imports lbGlobalcom
Public Class fListaSedes
    Dim daCiudades As lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
    Private lIdSede As String
    Public ReadOnly Property Sede() As String
        Get
            Return lIdSede
        End Get
    End Property

    Private Sub CargaCiudades()
        Try
            daCiudades.Fill(DsGlobal.GLO_MUNICIPIOS)
            Dim Dr As lbGlobalcom.dsGlobal.GLO_MUNICIPIOSRow = DsGlobal.GLO_MUNICIPIOS.NewGLO_MUNICIPIOSRow
            Dr.IDMUNICIPIOS = "-1"
            Dr.MUNICIPIOS = "Todas"
            DsGlobal.GLO_MUNICIPIOS.Rows.Add(Dr)
            Dr = Nothing
            cbCiudad.SelectedValue = "-1"
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub

    Private Sub btnConsulta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConsulta.Click
        Dim Conn As OracleConnection = New OracleConnection(My.Settings.ConnectionString)
        Dim cmd As OracleCommand = Conn.CreateCommand
        Dim CuentaW As Integer
        Dim CadSql As String = "SELECT * FROM GLO_SEDES "
        Dim qWhere As String = " WHERE "
        If Me.txtSede.Text.Trim.Length > 0 Then
            CuentaW += 1
            qWhere &= "NOMBRE LIKE '%" & Me.txtSede.Text & "%'"
        End If
        If Me.cbCiudad.SelectedValue <> "-1" Then
            If CuentaW > 0 Then _
                qWhere &= " AND "
            CuentaW += 1
            qWhere &= "CIUDAD = " & cbCiudad.SelectedValue
        End If

        If CustomPrincipal.IDSede.Length > 0 Then
            Dim CAMPO As String = CustomPrincipal.GetFiltroSede("IDSEDE")
            If CuentaW > 0 Then _
                qWhere &= " AND " & CAMPO
            CuentaW += 1
            qWhere &= CAMPO
        End If

        If CuentaW > 0 Then _
            CadSql &= qWhere
        Try
            cmd.CommandText = CadSql
            Conn.Open()
            DsGlobal.GLO_SEDES.Rows.Clear()
            DsGlobal.GLO_SEDES.Load(cmd.ExecuteReader)
            Conn.Close()
            GLO_SEDESDataGridView.Focus()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            lbGlobalcom.cError.SalvaError(ex, "Henry")
        End Try
    End Sub


    'Private Sub GLO_SEDESDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs)
    '    If e.RowIndex >= 0 Then
    '        lIdSede = GLO_SEDESDataGridView.Rows(e.RowIndex).Cells("IDSEDE").Value
    '        Me.DialogResult = Windows.Forms.DialogResult.OK
    '    End If
    'End Sub

    Private Sub fListaSedes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        daCiudades = New lbGlobalcom.dsGlobalTableAdapters.GLO_MUNICIPIOSTableAdapter
        CargaCiudades()
    End Sub

    Private Sub GLO_SEDESDataGridView_CellMouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles GLO_SEDESDataGridView.CellMouseDoubleClick
        If e.RowIndex >= 0 Then
            lIdSede = GLO_SEDESDataGridView.Rows(e.RowIndex).Cells("IDSEDE").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub AsignaSedes(ByVal Fila As Integer)
        If Fila >= 0 Then
            lIdSede = GLO_SEDESDataGridView.Rows(Fila).Cells("IDSEDE").Value
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub GLO_SEDESDataGridView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GLO_SEDESDataGridView.KeyDown
        If e.KeyData = Keys.Enter Then
            AsignaSedes(GLO_SEDESDataGridView.CurrentRow.Index)
        End If
    End Sub
End Class